﻿import D3D.Vertex;
import D3D.Matrix;
import D3D.Polygon;
import D3D.Style;
class D3D.Screen {
	//---------------------------
	public var matrix:Matrix;
	public var style:Style;
	public var center:Vertex;
	public var fixVertexC:Vertex;
	public var polygons:Object;
	private var tag:MovieClip;
	private var me:Object;
	//----------------------------- 构造函数
	public function Screen(screenMc:MovieClip) {
		if (screenMc == undefined || eval(screenMc) == undefined) {
			say("场景创建目标不正确 "+screenMc);
			return;
		} else {
			tag = screenMc;
		}
		center=new Vertex();
		fixVertexC=new Vertex();
		polygons = new Array();
		matrix = new Matrix();
		me = this;
	}
	//----------------------------- 功能函数
	///
	//旋转所有polygon一定角度
	///
	public function Rotation(x,y,z,p)
	{
		if(p==undefined){
			for(var i in polygons)
			{
				polygons[i].RotationCenter(x,y,z);
				//polygons[i].RotationCenter(x,y,z);
		//		polygons[i].Rotation(x,y,z);
			}
		}else if(p.pname!=undefined){
			//p.center=matrix.zRotation(p.center,x,y,z);
			p.RotationCenter(x,y,z);
	//			polygons.Rotation(x,y,z);
			
		}else if(p[0]==undefined)
		{
			for(var i in p)
			{
				if(me.polygons[p[i]]!=undefined)
				{
					//me.polygons[p[i]].center=matrix.zRotation(me.polygons[p[i]].center,x,y,z);
					me.polygons[p[i]].RotationCenter(x,y,z);
	//				me.polygons[p[i]].Rotation(x,y,z);
					
				}
			}
		}
	}
	///
	//旋转场情一定角度
	//
	public function RotationCenter(x,y,z){
		fixVertexC = fixVertexC.plus (new Vertex (x, y, z));
		
	}
	public function Move(x,y,z,p)
	{
		if(p==undefined){
			for(var i in polygons)
			{
				//polygons[i].center=matrix.zMove(polygons[i].center,x,y,z);
				polygons[i].MoveCenter(x,y,z);
			}
		}else if(p.pname!=undefined){
			//p.center=matrix.zMove(p.center,x,y,z);
			p.MoveCenter(x,y,z);
		}else if(p[0]==undefined)
		{
			for(var i in p)
			{
				if(me.polygons[p[i]]!=undefined)
				{
					//me.polygons[p[i]].center=matrix.zMove(me.polygons[p[i]].center,x,y,z);
					me.polygons[p[i]].MoveCenter(x,y,z);
				}
			}
		}
	}
	public function addPolygon(polys) {
		if (polys.length>0) {
			for (var i in polys) {
				me.addProperty(polys[i]);
			}
		} else if (polys.pname != undefined) {
			for (var i in me.polygons) {
				if (polys.pname == i) {
					say("多边形名重复! "+polys.pname);
					return false;
				}
			}
			me.polygons[polys.pname] = polys;
			createPolygonMovieClip(polys.pname);
			polys.screen = me;
		}
		return true;
	}
	private function createPolygonMovieClip(name:String):MovieClip{
		var pldepth:Number=tag.getNextHighestDepth();
		if(pldepth>1040000)
		{
			pldepth=0;
		}
		tag.createEmptyMovieClip(name,pldepth);
		var plmovie:MovieClip=tag[name];
		return plmovie;
	}
	public function DrawScreen() {
		tag.jsq = 0;
		tag.objList = new Array();
		clears(tag);
		for (var i in polygons) {
			
			var polyV:Array=polygons[i].localToScreen();
	//		polyV[0].toString();
			polyV=me.localToScreen(polyV);
	//		trace("///");
	//		polyV[0].toString();
	//		trace("//////////////\r\r")
			polygons[i].Draw(tag,polyV);
			/*
			for(var l in polygons[i].child)
			{
				trace("draw child:"+l);
				var polyV2:Array=polygons[i].child[l].localToScreen();
				polyV2=me.localToScreen(polyV2);
				polygons[i].child[l].Draw(tag,polyV2);
			}
			*/
		}
		//setDepth();
	}
	public function localToScreen (v)
	{
		if (v != undefined)
		{
			if (v [0].x != undefined)
			{
				var rv : Array = new Array ();
				var i = 0;
				while (i < v.length)
				{
					rv.push (me.localToScreen (v [i]));
					i ++;
				}
				return rv;
			} else if (v.x != undefined)
			{
				var rvs : Vertex = new Vertex ();
				rvs = me.matrix.zRotation (v, fixVertexC.x, fixVertexC.y, fixVertexC.z);
				rvs = rvs.plus (me.center);
				/*
				if(me.screen==undefined){
				rvs=rvs.plus(me.screen.center);
				}
				*/
				return (rvs);
			}
		}
	}
	public function toString(){
		trace("----  Screen  ----");
		trace("style:");
		me.style.toString();
		trace("");
		trace("matrix:");
		me.matrix.toString();
		trace("");
		trace("polygon:");
		for(var i in polygons)
		{
			trace(i+":");
			polygons[i].toString();
		}
		trace("------------------");
	}
	private function setDepth():Boolean {
		var maxd:Number=-1;
		var dl:Array=getDepthList();
		
		for(var i=0;i<dl.length;i++)
		{
			if(eval(tag[dl[i].name])!=undefined)
			{
				tag[dl[i].name].swapDepths(10000-i);
			}else{
				say("设置polygon深度时发现未定义的目标。");
			}
		}
		return true;
	}
	//返回深度从高到低排列POLYGON数组
	private function getDepthList():Array{
		var a:Array=new Array();
		for(var i in polygons)
		{
			a.push(polygons[i]);
			for(var l=1;l<a.length;l++)
			{
				if(a[l].z<polygons[i])
				{
					for(var n=l;n<a.length;n++)
					{
						var temp=a[n];
						a[n]=a[(a.length-1)];
						a[(a.length-1)]=temp;
					}
				}
			}
		}
		return a;
	}
	private function clears (tag, lists)
	{
		tag.clear ();
		//trace (tag);
		if (lists == undefined)
		{
			lists = new Array ();
		}
		lists.push (tag);
		for (var i in tag)
		{
			if (typeof (tag [i]) == "movieclip" )
			{
				var b = true;
				for (var l in lists)
				{
					if (tag [i] == lists [l])
					{
						b = false;
						break;
					}
				}
				if (b)
				{
					me.clears (tag [i] , lists);
				}
			}
		}
	}
	private function say(o:Object) {
		trace("Screen:"+o);
	}
}
